package fr.neyb.bernouilli.problem017;

import java.util.regex.Pattern;

public class Entry {

	private static final int limit = 1000;
	private static final Pattern replacer = Pattern.compile("[- ]");

	public static void main(String... args) throws Exception {
		int count = 0;
		for (int i = 1; i < limit; ++i) {
			count += count(i);
			System.out.println(i + " :\t" + Spell_1000(i) + "\t"
					+ replacer.matcher(Spell_1000(i)).replaceAll("") + "\t"
					+ count(i) + "\t" + count);
		}

		count += replacer.matcher("one thousand").replaceAll("").length();
		System.out.println(count);
	}

	private static String[] digits = new String[] { "zero", "one", "two",
			"three", "four", "five", "six", "seven", "eight", "nine", "ten",
			"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
			"seventeen", "eighteen", "nineteen" };
	private static String[] decade = new String[] { null, null, "twenty",
			"thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

	private static String hundred = "hundred";

	private static int count(int i) {
		return replacer.matcher(Spell_1000(i)).replaceAll("").length();
	}

	private static String Spell_1000(int i) {
		StringBuilder res = new StringBuilder();

		if (i >= 100)
			res.append(Spell_20(i / 100)).append(" ").append(hundred);

		if ((i %= 100) > 0) {
			if (res.length() > 0)
				res.append(" and ");

			res.append(spell_100(i));
		}
		return res.toString();
	}

	private static String spell_100(int i) {
		StringBuilder res = new StringBuilder();

		if (res.length() > 0)
			res.append(" and ");
		if (i >= 20) {
			res.append(decade[i / 10]);

			if ((i %= 10) > 0)
				res.append("-").append(Spell_20(i));

		} else {
			res.append(Spell_20(i));
		}

		return res.toString();
	}

	private static String Spell_20(int i) {
		return digits[i];
	}
}
